<?php

namespace App\Commons\Providers;

use App\Commons\Libs\RequestExtension;
use Illuminate\Support\ServiceProvider;
use Request;
use Symfony\Component\Finder\Finder;
use Str;
use Arr;

class RequestProvider extends ServiceProvider
{
    /**
     * Bootstrap services.
     *
     * @return void
     */
    public function boot()
    {
    }

    /**
     * Register services.
     *
     * @return void
     */
    public function register()
    {
        Request::macro(
            'fields',
            function (array $arguments, bool $remove_keys = true) {
                static $request_extend;
                if (!$request_extend) {
                    $request_extend = new RequestExtension($this);
                }
                $result = $request_extend->get($arguments);
                return !$remove_keys ? $result : array_values($result);
            }
        );

        Request::macro(
            'params',
            function ($param_class = null) {
                static $request_extend;
                if (!$request_extend) {
                    $request_extend = new RequestExtension($this);
                }
                $result = $request_extend->values();
                if (blank($param_class)) {
                    return $result;
                } elseif ($param_class === false) {
                    return array_values($result);
                } elseif (is_array($param_class)) {
                    $res = [];
                    foreach ($param_class as $value) {
                        $res[] = isset($result[$value]) ? $result[$value]: null;
                    }
                    return $res;
                } elseif (class_exists($param_class)) {
                    return new $param_class($result);
                }
            }
        );

        Request::macro(
            'values',
            function () {
                static $request_extend;
                if (!$request_extend) {
                    $request_extend = new RequestExtension($this);
                }
                $result = $request_extend->values();

                return array_values($result);
            }
        );

        Request::macro(
            'values',
            function () {
                return array_values($this->params());
            }
        );

        Str::macro(
            'mustacheReplace',
            function (string $subject, array $replacements) {
                return preg_replace_callback(
                    "/{{([^{}]+)}}/",
                    function ($matches) use ($replacements) {
                        $matche = $matches[1];
                        foreach ($replacements as $key => $value) {
                            if ($key === $matche) {
                                return $value;
                            }
                        }
                        return $matches[0];
                    },
                    $subject
                );
            }
        );
    }
}
